package cn.xerllent.wxtp.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.util.ObjectUtil;
import cn.xerllent.common.entity.vo.OffsetPageable;
import cn.xerllent.common.service.impl.BaseServiceImpl;
import cn.xerllent.wxtp.entity.Dksb;
import cn.xerllent.wxtp.entity.DksbCourse;
import cn.xerllent.wxtp.service.DksbCourseService;
import cn.xerllent.wxtp.service.DksbService;
import cn.xerllent.wxtp.service.dto.DksbFilter;
import cn.xerllent.wxtp.service.mapper.DksbMapper;
import cn.xerllent.common.utils.FileUtil;

@Service
//@AllArgsConstructor
//@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DksbServiceImpl extends BaseServiceImpl<DksbMapper, Dksb>  implements DksbService {
	
	private DksbCourseService dksbCourseService;

    @Override
    //@Cacheable
    public List<Dksb> queryAll(DksbFilter filter, OffsetPageable offsetPageable) {
    	PageHelper.offsetPage(offsetPageable.getOffset(), offsetPageable.getPageSize(), false);//false 表示不需要count查询
        PageInfo<Dksb> page = new PageInfo<>(queryAll(filter));
        return page.getList();
    }

    @Override
    //@Cacheable
    public List<Dksb> queryAll(DksbFilter filter) {
    	//方式1
//    	List<Dksb> list = baseMapper.queryWithExt(filter);
//        for (Dksb dksb : list) {
//        	dksb.setCourses(dksbCourseService.list(new LambdaQueryWrapper<DksbCourse>().eq(DksbCourse::getDksbId, dksb.getId())));
//        }
        
        //方式2
        List<Dksb> list = baseMapper.queryWithCourse(filter); //直接含子集
        
        return list;
    }

    @Override
    public void download(List<Dksb> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Dksb Dksb : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("id", Dksb.getId());
            map.put("企业id", Dksb.getCorpId());
            map.put("申报人", Dksb.getReqUserid());
            map.put("申报事由", Dksb.getComment());
            map.put("审批流程单号", Dksb.getThirdNo());
            map.put("审批流程模板", Dksb.getTemplateId());
            map.put("审批状态", Dksb.getStatus());
            map.put("创建时间", Dksb.getCreatetime());
            map.put("变动时间", Dksb.getUpdatetime());
            map.put("审核时间", Dksb.getAudittime());
            map.put("代课列表", Dksb.getCourses());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    
	/**
	 * 清除对应Mapper的二级缓存，采用无害删除空数据模拟清空,因为二级缓存更新机制是本表数据更新即可清空<br>
	 * 提供本方法供联表更新查询的实时更新
	 * */
	@Override
	public void clearCache() {
		baseMapper.deleteById(0);
	}

}
